package gui.services;

import gui.dao.ConfigDAO;
import gui.entity.Config;

import java.util.concurrent.Callable;

/**
 * @author:feixiong
 * @Date: 2021/1/25
 */
public class ConfigService {
    public static final String budget = "budget";
    public static final String mysqlPath="mysqlPath";
    public static final String default_budget="500";
    static ConfigDAO dao=new ConfigDAO();
    static{
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void init() throws Exception {
        init(budget,default_budget);
        init(mysqlPath,"");
    }
    public static void init(String key,String value) throws Exception {
        Config config= dao.getByKey(key);
        if(config == null){
            Config c =new Config();
            c.setKey(key);
            c.setValue(value);
           dao.add(config);
                }
        }
        public String get(String key) throws Exception {
        Config config=dao.getByKey(key);
         return config.getValue();
        }
        public void update(String key,String value) throws Exception {
        Config config=dao.getByKey(key);
        config.setValue(value);
        dao.update(config);
        }
        public  int getIntBudget() throws Exception {
        return Integer.parseInt(get(budget));
        }
        }




